package com.ming.stringstudy.feedbacksystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ming.stringstudy.feedbacksystem.anno.PermissionLog;
import com.ming.stringstudy.feedbacksystem.entity.Dept;
import com.ming.stringstudy.feedbacksystem.mapper.CommentTypeMapper;
import com.ming.stringstudy.feedbacksystem.mapper.DeptMapper;
import com.ming.stringstudy.feedbacksystem.mapper.UserMapper;
import com.ming.stringstudy.feedbacksystem.pojo.PageBean;
import com.ming.stringstudy.feedbacksystem.service.IDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.stringstudy.feedbacksystem.utils.PermissionConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aming
 * @since 2025-01-19
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private CommentTypeMapper commentTypeMapper;
    @Autowired
    private UserMapper userMapper;

    @PermissionLog(PermissionConstants.DEPT_MANGE)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageBean<Dept> page(Integer page, Integer pageSize, Dept dept) {
        PageHelper.startPage(page, pageSize);

        List<Dept> deptList = deptMapper.findByAttributes(dept);

        PageInfo<Dept> pageInfo = new PageInfo<>(deptList);
        PageBean<Dept> pageBean = new PageBean<>(pageInfo.getTotal(), pageInfo.getList());
        return pageBean;
    }
    @PermissionLog(PermissionConstants.DEPT_MANGE)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addDept(Dept dept) {
        if (dept.getDeptName() == null || dept.getDeptName().equals("")) {
            return 0;
        }
        if (deptMapper.findAllByDeptName(dept.getDeptName()) != null){
            return 0;
        }

        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());

        int result = deptMapper.insert(dept);
        return result;
    }
    @PermissionLog(PermissionConstants.DEPT_MANGE)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDept(Dept dept) {
        if (deptMapper.findAllByDeptName(dept.getDeptName()) != null){
            return 0;
        }
        dept.setUpdateTime(LocalDateTime.now());
        int result = deptMapper.updateById(dept);
        return result;
    }
    @PermissionLog(PermissionConstants.DEPT_MANGE)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteDept(List<Integer> ids) {
        int result = deptMapper.deleteBatchIds(ids);
        commentTypeMapper.updateByDeletedDeptId(ids);
        userMapper.updateUserByDeletedDeptId(ids);
        return result;
    }

}
